Welcome to “Python Style Guide and Coding
Practices.” After watching this video, you’ll be able
to: Describe the importance of writing readable
code List the Python coding conventions Explain static code analysis When you write code, you need to ensure the
team can read and understand it easily. This task requires some coding standards and
conventions. Python.org has released a document called
Python Enhancement Proposal 8 (or PEP8) that provides conventions and guidelines to make
your Python code readable and consistently formatted. Let’s start with the key guidelines for
improving the readability of the code. PEP8 recommends the use of spaces instead
of tabs for indentation. Wondering why? Different text editors and integrated development
environments, or IDEs, interpret the number of spaces in a tab differently. For example, one editor might interpret a
tab as three spaces, while another might interpret it as four. Using a tab for indentation can lead to non-uniformity
in code, resulting in formatting errors. For example, observe the spacing difference
when Tab is pressed for statement 1 for the if condition under Editor 1 when compared
with the space before statement 1 for the if condition under Editor 2. To avoid such errors, you should use a consistent
number of spaces when indenting your code. For uniformity, the guideline is to use four
spaces for every indentation level in a code. Four spaces for an indentation are sufficient
for appropriate readability. Please note that the four dots have been added
before statement one and statement two to depict four spaces. PEP8 also recommends using blank lines to
separate functions and classes in the code. Notice that in the left block, which does
not follow PEP8 guidelines, there is no space between where function one ends and where
class UserClass, capital U and capital C no space. However, the right side shows the correct
representation with a space before the class definition. The blank lines help establish the beginning
and end of different parts of the code. To improve code readability, use spaces around
operators and after commas. Using spaces will make the commands look spacious
and discrete, improving command readability. Let’s look at some examples. When you write “A” equals b plus c without
any spaces, it can be confusing. However, when you add the spaces, for example
“A” space equals space b space plus space c, readability improves. Next, let’s look at some coding conventions
to maintain consistency and efficient manageability. A key coding convention is to create separate
functions for functionalities with larger blocks of code. You can then call the functions from the main
program. For example, in the code, the if else syntax
does not have a function and will need to be written every time you need the functionality. However, if you notice function underscore
one written as def function underscore one with two parameters a and b, it can be called
easily. For example, c equals function underscore
one open round bracket “A” comma b close round bracket. This increases the execution speed of the
code and supports the reuse of the code block in a much more convenient way. Another coding convention is to name the functions
and files using lowercase with underscores. Wondering why? Python uses the naming convention and has
many built-in libraries and predefined functions that use the common naming conventions. Therefore, using a lowercase function name,
preferably with underscores, is advisable to make the functions unique. For example, instead of writing function name
as comp Surface with capital S Radiation with capital R open round bracket close round bracket,
write comp underscore surface underscore radiation open round bracket close round bracket. An exception to the rule is the naming criteria
for Python packages, where underscores are generally discouraged. For example, instead of writing my underscore
package, write mypackage. Naming the classes using CamelCase is also
a coding convention. CamelCase, or the CapWord case, is a naming
convention for classes that is well-accepted in the coding community. CamelCase also helps distinguish between classes
and functions in the code. For example, instead of writing class Lam
underscore squirrel underscore Cage, the best practice is to name the class as Lam Squirrel
Cage with the initial L, S, and C in capital letters. Use the coding convention of capitalizing
all words and separating them with underscores for consistency. The name usually indicates the constant’s
purpose, for example, MAX_FILE_UPLOAD_SIZE in capital letters. We discussed the coding convention and guidelines. Software developers commonly use static code
analysis to manage compliance with these style guidelines. Static code analysis is a method to evaluate
code against a predefined style and standard without executing the code. Static analysis helps find issues, such as: Programming errors, Coding standard violations, Undefined values, Syntax violations, and Security vulnerabilities. You can use the PyLint library to check the
compliance of your Python code with PEP8 guidelines. In this video, you learned that: Writing consistent code helps all team members
read and understand the code easily. The PEP8 guidelines for better code readability
include the following: Four spaces for indentation, Use blank lines to separate functions and
classes, and Use spaces around operators and after commas. Some coding conventions for code consistency
and manageability include: Add larger blocks of code inside functions, Name functions and files using lowercase with
underscores, Name classes using CamelCase, and Name constants in all capital letters with
underscores separating words. Use Static code analysis method to evaluate
your code against a predefined style and standard without executing the code.